package cn.izeno.wx.models.mp

class KfMessageBuilder(val accessToken: String) {

  fun toUser(openid: String): MessageTypeBuilder {
    return MessageTypeBuilder(accessToken, openid)
  }

  class MessageTypeBuilder internal constructor(
      private val accessToken: String,
      private val openid: String
  ) {

    /**
     * - 在输入状态中（持续15s），不可重复下发输入状态
     * - 45081	已经在输入状态，不可重复下发
     * - 45080	下发输入状态，需要之前30秒内跟用户有过消息交互
     */
    fun typing(): KfMessage {
      val kfMsg = KfMessage()
      kfMsg["accessToken"] = accessToken
      kfMsg["touser"] = openid
      kfMsg["command"] = "Typing"

      return kfMsg
    }


    fun cancelTyping(): KfMessage {
      val kfMsg = KfMessage()
      kfMsg["accessToken"] = accessToken
      kfMsg["touser"] = openid
      kfMsg["command"] = "CancelTyping"

      return kfMsg
    }

    /**
     * 发送文本消息时，支持插入跳小程序的文字链文本内容
     *
     * ```
     * <a href="http://www.qq.com"
     *   data-miniprogram-appid="appid"
     *   data-miniprogram-path="pages/index/index">
     *   点击跳小程序
     * </a>
     * ```
     * - 小程序必须与公众号有绑定关系
     * - href 不支持小程序的客户端会跳转这个链接
     * - data-miniprogram-path 可带参数
     */
    fun text(content: String): KfMessage {
      val type = "text"
      val kfMsg = KfMessage()
      kfMsg["accessToken"] = accessToken
      kfMsg["touser"] = openid
      kfMsg["msgtype"] = type

      val text = HashMap<String, String>()
      text["content"] = content
      kfMsg[type] = text

      return kfMsg;
    }

    fun image(mediaId: String): KfMessage {
      val type = "image"
      val kfMsg = KfMessage()
      kfMsg["accessToken"] = accessToken
      kfMsg["touser"] = openid
      kfMsg["msgtype"] = type

      val msgBody = HashMap<String, String>()
      msgBody["media_id"] = mediaId
      kfMsg[type] = msgBody

      return kfMsg;
    }

    fun voice(mediaId: String): KfMessage {
      val type = "voice"
      val kfMsg = KfMessage()
      kfMsg["accessToken"] = accessToken
      kfMsg["touser"] = openid
      kfMsg["msgtype"] = type

      val msgBody = HashMap<String, String>()
      msgBody["media_id"] = mediaId
      kfMsg[type] = msgBody

      return kfMsg;
    }

    fun video(mediaId: String, thumbMediaId: String, title: String, desc: String): KfMessage {
      val type = "video"
      val kfMsg = KfMessage()
      kfMsg["accessToken"] = accessToken
      kfMsg["touser"] = openid
      kfMsg["msgtype"] = type

      val msgBody = HashMap<String, String>()
      msgBody["media_id"] = mediaId
      msgBody["thumb_media_id"] = thumbMediaId
      msgBody["title"] = title
      msgBody["description"] = desc
      kfMsg[type] = msgBody

      return kfMsg;
    }

    fun music(musicUrl: String, hqMusicUrl: String, thumbMediaId: String, title: String, desc: String): KfMessage {
      val type = "music"
      val kfMsg = KfMessage()
      kfMsg["accessToken"] = accessToken
      kfMsg["touser"] = openid
      kfMsg["msgtype"] = type

      val msgBody = HashMap<String, String>()
      msgBody["musicurl"] = musicUrl
      msgBody["hqmusicurl"] = hqMusicUrl
      msgBody["thumb_media_id"] = thumbMediaId
      msgBody["title"] = title
      msgBody["description"] = desc
      kfMsg[type] = msgBody

      return kfMsg;
    }

    /**
     * 图文链接
     * @param picUrl 尺寸推荐 900 x 500
     */
    fun news(title: String, desc: String, url: String, picUrl: String): KfMessage {
      val type = "news"
      val kfMsg = KfMessage()
      kfMsg["accessToken"] = accessToken
      kfMsg["touser"] = openid
      kfMsg["msgtype"] = type

      val article = HashMap<String, String>()
      article["title"] = title
      article["description"] = desc
      article["url"] = url
      article["picurl"] = picUrl

      val news = HashMap<String, Any>()
      news["articles"] = listOf(article)

      kfMsg[type] = news

      return kfMsg;
    }

    /**
     * @param thumbMediaId 建议大小 520 * 416
     */
    fun miniProgramPage(title: String, appid: String, path: String, thumbMediaId: String): KfMessage {
      val type = "miniprogrampage"

      val kfMsg = KfMessage()
      kfMsg["accessToken"] = accessToken
      kfMsg["touser"] = openid
      kfMsg["msgtype"] = type

      val msgBody = HashMap<String, String>()
      msgBody["title"] = title
      msgBody["appid"] = appid
      msgBody["pagepath"] = path
      msgBody["thumb_media_id"] = thumbMediaId
      kfMsg[type] = msgBody

      return kfMsg;
    }
  }
}